#include "factory.h"
#include "config_api.h"
#include "factory_api.h"
#include "led_test.h"
#include "uc_log.h"
#include "wiota_test.h"
#include <rtthread.h>
#define DBG_LVL DBG_LOG
#include "file_test.h"
#include "manager_task.h"
#include "network_test.h"
#include "rtc_test.h"
#include "uart_test.h"
#include <rtdbg.h>

int result = FACTORY_TEST_DEF_VALUE;
// int result1 = FACTORY_TEST_DEF_VALUE; // sd test
int result2 = FACTORY_TEST_DEF_VALUE;
int result3 = FACTORY_TEST_DEF_VALUE;
int result4 = FACTORY_TEST_DEF_VALUE;
int result5 = FACTORY_TEST_DEF_VALUE;
int result7 = FACTORY_TEST_DEF_VALUE;

void facotry_result_output(int type, int result, unsigned int start_time)
{
    char remark[REMARK_LEN] = {0};
    char *test_res[2] = {"suc", "fail"};

    if (result == 0)
    {
        LOG_I("@%s test %s, time %d ms", factory_get_key(type), test_res[result], rt_tick_get() - start_time);
    }
    else
    {
        factory_get_result(type, RT_NULL, RT_NULL, remark);
        LOG_I("@%s test %s, %s, time %d ms", factory_get_key(type), test_res[1], remark, rt_tick_get() - start_time);
    }
}

unsigned int facotry_start_output(int type)
{
    LOG_I("@%s test start", factory_get_key(type));
    return rt_tick_get();
}

// 网络测试任务
static void factory_network_test_task(void *para)
{
    LOG_D("%s line %d", __FUNCTION__, __LINE__);
    network_test_init();

    // 网络测试
    result4 = network_test();
}

// 485测试任务和蓝牙测试任务
static void factory_485_bt_test_task(void *para)
{
    unsigned int current_time = 0;
    LOG_D("%s line %d", __FUNCTION__, __LINE__);
    // 蓝牙mac地址获取
    result7 = fac_bt_mac_addr_get();

    // 485 测试
    current_time = facotry_start_output(UART485_TEST);
    result3 = fac_485_uart_test();
    facotry_result_output(UART485_TEST, result3, current_time);
    rt_thread_mdelay(30);

    // 蓝牙测试
    if (result7 == RT_EOK)
    {
        current_time = facotry_start_output(BLUETOOTH_TEST);

        result2 = fac_ble_uart_test();
        facotry_result_output(BLUETOOTH_TEST, result2, current_time);
        // rt_thread_mdelay(30);
    }
    else
    {
        result2 = result7;
        facotry_result_output(BLUETOOTH_TEST, result7, current_time);
    }
}

// 存储和rtc工厂测试任务
static void factory_storage_rtc_test(void *para)
{
    unsigned int current_time = 0;
    LOG_D("%s line %d", __FUNCTION__, __LINE__);
    // 外部flash 测试
    current_time = facotry_start_output(EXTERNAL_FLASH_TEST);

    result = file_wr_test(EXTERNAL_FLASH_TEST, "/logs/flash.txt");
    facotry_result_output(EXTERNAL_FLASH_TEST, result, current_time);

    // SD 卡测试
    // result1 = file_wr_test(SD_TEST, "/sd/test.txt");
    // facotry_result_output(SD_TEST, result1);

    // RTC测试
    current_time = facotry_start_output(RTC_TEST);

    result5 = factory_rtc_test();
    facotry_result_output(RTC_TEST, result5, current_time);
}

int factory_test(void *info)
{
    int flag = 1;
    gw_auth_cfg_t *auth_cfg = info;
    void *tmp_handle = RT_NULL;
    unsigned int current_time = 0;

    LOG_D("factory_test start");
    current_time = facotry_start_output(FACTORY_MODE);

    // 设置测试模式
    auth_cfg->work_mode = UC_NORMAL_MODE;
    uc_static_set_gateway_auth_cfg(auth_cfg);
    uc_static_save_op();

    // WIoTa测试
    wiota_test();

    factory_led_init();

    led_all_switch(0);

    // 网络测试任务
    if (0 != manager_thread_create(&tmp_handle, "net_fac", factory_network_test_task, RT_NULL, 1024, RT_THREAD_PRIORITY_MAX / 5, 5))
    {
        TRACE_E("factory network test task create fail");
    }

    rt_thread_startup((rt_thread_t)tmp_handle);

    // 485测试任务和蓝牙测试任务
    if (0 != manager_thread_create(&tmp_handle, "bt_fac", factory_485_bt_test_task, RT_NULL, 1024, RT_THREAD_PRIORITY_MAX / 5, 5))
    {
        TRACE_E("factory 485/bt test task create fail");
    }

    rt_thread_startup((rt_thread_t)tmp_handle);

    // 存储和rtc工厂测试任务
    if (0 != manager_thread_create(&tmp_handle, "rtc_fac", factory_storage_rtc_test, RT_NULL, 1024, RT_THREAD_PRIORITY_MAX / 5, 5))
    {
        TRACE_E("factory flash/rtc test task create fail");
    }

    rt_thread_startup((rt_thread_t)tmp_handle);

    facotry_result_output(FACTORY_MODE, 0, current_time);

    // 等待按键服务测试
    while (1)
    {
        if ((result != FACTORY_TEST_DEF_VALUE) &&
            (result2 != FACTORY_TEST_DEF_VALUE) &&
            (result3 != FACTORY_TEST_DEF_VALUE) &&
            (result4 != FACTORY_TEST_DEF_VALUE) &&
            (result5 != FACTORY_TEST_DEF_VALUE) &&
            (wiota_test_result(WIOTA_TEST) != FACTORY_TEST_DEF_VALUE) &&
            (wiota_test_result(GPS_TS_TEST) != FACTORY_TEST_DEF_VALUE) &&
            (wiota_test_result(SYNC_TS_TEST) != FACTORY_TEST_DEF_VALUE) &&
            flag)
        {
            LOG_D("factory test complete, please input led_save");
            flag = 0;
        }
        else
        {
            LOG_D("result %d %d %d %d %d %d %d %d",
                  result, result2, result3, result4, result5,
                  wiota_test_result(WIOTA_TEST),
                  wiota_test_result(GPS_TS_TEST),
                  wiota_test_result(SYNC_TS_TEST));
        }

        // 根据测试结果led状态全亮或闪烁
        if ((result == 0) &&
            (result2 == 0) &&
            (result3 == 0) &&
            (result4 == 0) &&
            (result5 == 0) &&
            (wiota_test_result(WIOTA_TEST) == 0) &&
            (wiota_test_result(GPS_TS_TEST) == 0) &&
            (wiota_test_result(SYNC_TS_TEST) == 0))
        {
            led_all_switch(1);
            rt_thread_mdelay(3000);
        }
        else
        {
            led_all_blink();
        }
    }
    return 0;
}
